import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.util.HashMap;

public class RecordMapper {
    private String qname;
    private String mappedIP = "x";
    private DatagramPacket inPacket;
    private HashMap<String, String> inMap;
    private int type;
    public RecordMapper(DatagramPacket inPacket, HashMap<String, String> inMap) throws IOException {
        this.qname = getQName(inPacket);
        this.inMap = inMap;
        this.inPacket = inPacket;
        this.type = getType(inPacket);
    }

    public boolean isRecorded() throws IOException {
        mappedIP = getIPAddr();
        if(mappedIP.equals("x")){
            return false;
        }
        else {
            return true;
        }
    }

    public void sendResponse(DatagramSocket mSocket) throws IOException {
        ResponseBuilder rb = new ResponseBuilder(inMap, inPacket, mappedIP);
        byte[] payload = new byte[0];
        if(type == QueryType.A){
            payload = rb.buildA();
        }
        else if(type == QueryType.AAAA){
            payload = rb.buildAAAA();
        }
        SocketAddress remoteAddress = inPacket.getSocketAddress();
        DatagramPacket sendPacket = new DatagramPacket(payload, payload.length, remoteAddress);
        mSocket.send(sendPacket);
    }

    public String getIPAddr() throws IOException {
        File f = new File("relay.txt");
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
        String line = "";
        while((line = in.readLine()) != null){
            String addr = line.split(" ")[0];
            String fname = line.split(" ")[1];
            if(fname.equals(qname)){
                if(Server.dbgLvFlag>=1)
                    System.out.println(addr + " - " + fname);
                if(type == QueryType.AAAA && addr.equals("0.0.0.0")){
                    in.close();
                    return "::";
                }
                else if(type == QueryType.AAAA && addr.contains(":")){
                    in.close();
                    return addr;
                }
                else if(type == QueryType.A && addr.contains(".")){
                    in.close();
                    return addr;
                }
            }
        }
        in.close();
        return "x";
    }

    public String getQName(DatagramPacket packet){
        byte[] data = packet.getData();
        int i = 13, j = 0;
        while(data[i] != 0){
            i++;
        }
        byte[] qnameByte = new byte[i-13];
        i = 13;
        while(data[i] != 0){
            if(data[i] > 47) {
                qnameByte[j] = data[i];
                j++;
            }
            else {
                qnameByte[j] = '.';
                j++;
            }
            i++;
        }
        return new String(qnameByte);
    }

    public int getType(DatagramPacket packet){
        byte[] data = packet.getData();
        return data[packet.getLength()-3];
    }
}
